
import { Storage } from '@/utils/index';

const state = {
  storeList: [], // 门店列表
  storeId: '', // 门店id
  dealerCode: '', // 门店所属经销商
  deptListLoading: false, // 部门列表loading状态
  deptList: [], // 部门列表
  deptId: '', // 部门id
};

const getters = {
  storeList: (state) => state.storeList,
  storeId: (state) => {
    if (!state.storeId) {
      return Storage.getLoc('storeId');
    }
    return state.storeId;
  },
  dealerCode: (state) => state.dealerCode,
  deptListLoading: (state) => state.deptListLoading,
  deptList: (state) => state.deptList,
  deptId: (state) => state.deptId,
};

const mutations = {
  INIT_STORE_LIST: (state, list) => {
    state.storeList = list;
  },
  SET_STORE_ID: (state, id) => {
    Storage.setLoc({
      key: 'storeId',
      value: id,
    });
    state.storeId = id;
  },
  SET_DEALER_CODE: (state, code) => {
    state.dealerCode = code;
  },
  SET_DEPT_LIST_LOADING: (state, loading) => {
    state.deptListLoading = loading;
  },
  INIT_DEPT_LIST: (state, list) => {
    state.deptList = list;
  },
  SET_DEPT_ID: (state, id) => {
    state.deptId = id;
  },
};

const actions = {
  initStoreList: async ({ commit,getters }, param) => {
    try {
      const res = await getStoreList();
      let dataList = [];
      let storeId = '';
      let dealerCode = '';
      const { data } = res;
      if (Array.isArray(data) && data.length > 0) {
        dataList = data.map((item) => {
          return {
            ...item,
            label: item.bdMdNameCn,
            value: item.bdMdId,
          };
        });
        if(param && param.init === true) {
          dataList.unshift({
            label: '全部',
            value: '',
          });
          storeId = ''
          dealerCode = '';
        } else {
          storeId = dataList[0].bdMdId || '';
          dealerCode = dataList[0].bdMdDealerCode || '';
        }
      } else {
        dataList = [];
        storeId = '';
        dealerCode = '';
      }
      commit('INIT_STORE_LIST', dataList);
      // init时，如果已有门店id 则 使用已有门店id
      commit('SET_STORE_ID', getters.storeId ? getters.storeId : storeId);
      commit('SET_DEALER_CODE', dealerCode);
    } catch (err) {
      console.error(err);
      commit('INIT_STORE_LIST', []);
      commit('SET_STORE_ID', '');
      commit('SET_DEALER_CODE', '');
    }
  },
  setDealerCode: ({ commit }, code) => {
    commit('SET_DEALER_CODE', code);
  },
  setStoreId: ({ commit }, id) => {
    commit('SET_STORE_ID', id);
  },
  initDeptList: async ({ commit }, param) => {
    try {
      commit('SET_DEPT_LIST_LOADING', true);
      let Obj = {
        storeId: param.storeId
      }
      if (param.userName) {
        Obj.userName = param.userName
      }
      if (param.roleId) {
        Obj.roleId = param.roleId
      }
      const res = await getDeptList(Obj);
      commit('SET_DEPT_LIST_LOADING', false);
      let deptList = [];
      let deptId = '';
      const { data } = res;
      if (Array.isArray(data) && data.length > 0) {
        deptList = data;
        // deptList.unshift({
        //   deptId: '',
        //   deptName: '全部',
        //   userCount: deptList.reduce((pre, e) => {
        //     return pre + e.userCount;
        //   }, 0),
        // });
        deptId = deptList[0].deptId || '';
      } else {
        deptList = [];
        deptId = '';
      }
      commit('INIT_DEPT_LIST', deptList);
      commit('SET_DEPT_ID', deptId);
      // 缓存部门id，用于新建员工页面
      sessionStorage.setItem('deptId', deptId);
    } catch (err) {
      console.error(err);
      commit('SET_DEPT_LIST_LOADING', false);
      commit('INIT_DEPT_LIST', []);
      commit('SET_DEPT_ID', '');
    }
  },
  setDeptId: ({ commit }, id) => {
    commit('SET_DEPT_ID', id);
  },
};

export default {
  getters,
  state,
  mutations,
  actions,
};
